﻿@model List<ShoppingCart>

@{
    var strHp = new StringHelper();
    var imgHp = new ImageHelper();

    float exchangeRate = ViewBag.ExchangeRate;
    float totalPrice = 0;
    float totalQuantity = 0;
}

<table class="table table-bordered no-margin">
    <thead>
        <tr>
            <th colspan="2">Thông tin sản phẩm</th>
            <th class="text-right">Đơn giá</th>
            <th class="text-right">SL</th>
            <th class="text-right">Thành tiền (USD)</th>
            <th class="text-right">Thành tiền (VND)</th>
        </tr>
    </thead>
    <tbody>
        @foreach (ShoppingCart item in Model)
        {
            // Model
            Style style = item.Style;
            Product product = style.Product;

            // Price
            float quantityPricing = item.Quantity * style.UnitPrice;
            totalPrice += quantityPricing;
            totalQuantity += item.Quantity;

            // Image
            Property color = style.StyleProperties
                .Select(x => x.Property)
                .FirstOrDefault(x => x.PropertyType.NameId == "color");
            Image image = new Image();
            if (color != null)
            {
                image = product.Images
                    .Where(x => x.PropertyId == color.Id)
                    .OrderByDescending(x => x.Priority)
                    .FirstOrDefault();
            }
            else
            {
                image = product.Images
                    .OrderByDescending(x => x.Priority)
                    .FirstOrDefault();
            }
            string imageUrl = image.Url + "?w=100";
            string imageCss = "";

            if (!imgHp.Standard(image.Width, image.Height))
            {
                imageUrl = image.Url + "?w=100&h=100";
                imageCss = "no-padding";
            }

            <tr>
                <td width="60" class="no-padding">
                    <img class="img-responsive center-block @imageCss" src="@imageUrl" height="60" width="60" />
                </td>
                <td>
                    <div class="name">@product.Name - @product.Code</div>
                    <div>
                        @foreach (Property p in style.StyleProperties.Select(x => x.Property).OrderBy(x => x.Name))
                        {
                            <span class="property">
                                <span class="text">@p.PropertyType.Name:</span>
                                <span class="value">@p.Name</span>
                            </span>
                        }
                    </div>
                </td>
                <td class="text-right unitprice">
                    <div class="usd">@strHp.PriceFormat(style.UnitPrice, "usd")</div>
                    <div class="vnd">@strHp.PriceFormat(style.UnitPrice * exchangeRate, "vnd")</div>
                </td>
                <td class="text-right">
                    @item.Quantity
                </td>
                <td class="price">
                    @strHp.PriceFormat(quantityPricing, "usd")
                </td>
                <td class="price">
                    @strHp.PriceFormat(quantityPricing * exchangeRate, "vnd")
                </td>
            </tr>
        }
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3"></td>
            <td class="text-right">
                @totalQuantity
            </td>
            <td class="price">
                @strHp.PriceFormat(totalPrice, "usd")
            </td>
            <td class="price">
                @strHp.PriceFormat(totalPrice * exchangeRate, "vnd")
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                VAT(10%) @{ float vat = totalPrice * 10 / 100; }
            </td>
            <td class="price text-danger">
                + @strHp.PriceFormat(vat, "usd")
            </td>
            <td class="price text-danger">
                + @strHp.PriceFormat(vat * exchangeRate, "vnd")
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                Phí giao hàng
            </td>
            <td class="price text-danger">
                + 0
            </td>
            <td class="price text-danger">
                + 0
            </td>
        </tr>
        <tr>
            <td colspan="4" class="text-right">
                Khuyến mãi
            </td>
            <td class="price text-success">
                - 0
            </td>
            <td class="price text-success">
                - 0
            </td>
        </tr>
        <tr class="price-checkout">
            <td colspan="4" class="text-right">
                <strong>Tổng cộng</strong> @{ float checkoutPrice = totalPrice + vat; }
            </td>
            <td class="price">
                @strHp.PriceFormat(checkoutPrice, "usd")
            </td>
            <td class="price">
                @strHp.PriceFormat(checkoutPrice * exchangeRate, "vnd")
            </td>
        </tr>
    </tfoot>
</table>